package cn.itcast.dao.impl;

import java.util.UUID;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import cn.itcast.dao.StudentDao;
import cn.itcast.mapper.StudentMapper;
import cn.itcast.util.MybatisUtil;
import cn.itcast.vo.Student;

public class StudentDaoImpl implements StudentDao{
	private SqlSessionFactory sqlSessionFactory;

	public StudentDaoImpl() {
		sqlSessionFactory=MybatisUtil.getSqlSessionFactory();
	}

	@Override
	public Student getStudentByUsername(String username) {
		Student student = null;
		SqlSession sqlSession = sqlSessionFactory.openSession();
		StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
		student=studentMapper.getStudentByUsername(username);	
		sqlSession.close();
		return student;
		
	}
	

	@Override
	public void updateStudent(Student student) {
		SqlSession sqlSession=null;
		try{
		 sqlSession = sqlSessionFactory.openSession();
		StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
		studentMapper.updateStudent(student);
		sqlSession.commit();
		}catch (Exception e) {
			sqlSession.rollback();
			e.printStackTrace();
		}finally{
			sqlSession.close();
		}
		
	}

	@Override
	public String getTokenid(String tokenId) {
		String token_id = null;
		SqlSession sqlSession = sqlSessionFactory.openSession();
		StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
		token_id=studentMapper.getTokenid(tokenId);	
		sqlSession.close();
		return token_id;
	}
	
	


}
